﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.Collections.ObjectModel;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Shell;
using System.Collections.Generic;
using System.Threading;
using WeiboPlus.Models;

namespace WeiboPlus.Shell.ViewModel
{
    public abstract class CollectionVMBase<TData> : ViewModelBase<TData>
    {
        #region Properties


        protected ObservableCollection<TData> _items = new ObservableCollection<TData>();
        public ObservableCollection<TData> Items
        {
            get { return _items; }
            set
            {
                if (_items != value)
                {
                    _items = value;
                    RaisePropertyChanged(() => Items);
                }
            }
        }
        #endregion


        public CollectionVMBase()
        {
            Initialize();
        }

        protected void LoadFromStorage()
        {
            IsCachedDataLoad = false;
            if (IsolatedStorageSettings.ApplicationSettings.Contains("ITEMS" + typeof(TData).Name))
            {
                _items = IsolatedStorageSettings.ApplicationSettings["ITEMS" + typeof(TData).Name] as ObservableCollection<TData>;
                IsCachedDataLoad = true;
            }
        }

        protected void SaveData()
        {
            if (Items != null && Items.Count > 0)
                IsolatedStorageSettings.ApplicationSettings["ITEMS" + typeof(TData).Name] =
                    Items.Take(20).ToObservableCollection();
        }

        protected void PopupItems<T1, T2>(ObservableCollection<T1> items, IList<T2> data, bool insert, Action callback)
            where T1 : ViewModelBase<T2>, IComparable<T1>, new()
        {
            if (data != null && data.Count > 0)
            {
                var count = items.Count;
                foreach (var d in data)
                {
                    if (items.Any((i) =>
                            {
                                var equable1 = i.Item as IEquatable<T2>;

                                if (equable1 != null)
                                {
                                    return equable1.Equals(d);
                                }
                                else
                                {
                                    return i.Item.Equals(d);
                                }
                            }
                            ))
                        continue;

                    var viewmodel = new T1() { Item = d };

                    Thread.Sleep(100);
                    ExecuteOnUIThread((vm) =>
                    {
                        if (insert)
                            items.Insert(0, viewmodel);
                        else
                        {
                            if (items.Count > count && viewmodel.CompareTo(items.Last()) <= 0)
                            {
                                items.Add(viewmodel);
                                return;
                            }

                            if (items.Count > count)
                            {
                                for (int i = count; i < items.Count; i++)
                                {
                                    if (viewmodel.CompareTo(items[i]) > 0)
                                    {
                                        items.Insert(i, viewmodel);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                items.Add(viewmodel);
                                return;
                            }
                        }

                    }, viewmodel);

                }
            }
            Thread.Sleep(600);
            ExecuteOnUIThread(() =>
            {
                Loading = false;
                if (callback != null)
                    callback();
            });
        }

        protected void PopupItems<T1, T2>(ObservableCollection<T1> items, IList<T2> data, bool insert) where T1 : ViewModelBase<T2>, IComparable<T1>, new()
        {
            PopupItems<T1, T2>(items, data, insert, null);
        }
    }
}
